package com.vchello.controller.file;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baomidou.mybatisplus.plugins.Page;
import com.vchello.bean.file.Folder;
import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.service.file.FolderService;
import com.vchello.util.ToolUtil;
import com.vchello.util.Utility;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/folder")
public class FolderController extends BaseController {
	
	private static final String REDIRECT_LOGIN = "redirect:/login";
	private static final int TYPE = 1;   //标识项目文件(显示项目文件夹下的文件)
	
	@Autowired
	private FolderService folderService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Page<Folder> page, Model model){
		User user = getUser();
		if(null == user){
			return REDIRECT_LOGIN;
		}
		page = folderService.findPageFolderByUserId(page, user.getId(), new Folder());
		model.addAttribute("page", page);
		return "file/fileList";
		
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String list(Model model, Page<Folder> page, Folder folder, int type){
		User user = getUser();
		if(null == user){
			return REDIRECT_LOGIN;
		}
		folder.setUserId(user.getId());
		page = (type == TYPE) ? folderService.findPageChildrenFolder(page, folder.getParentId(), folder) : folderService.findPageFolderByUserId(page, user.getId(), folder);
		model.addAttribute("page", page).addAttribute("parentId", folder.getParentId()).addAttribute("type", type);
		return "file/query/folderQuery";
		
	}
	
	@RequestMapping(value = "/childrenFolder/{parentId}", method = RequestMethod.GET)
	public String childrenFolder(Model model, Page<Folder> page, @PathVariable int parentId){
		User user = getUser();
		if(null == user){
			return REDIRECT_LOGIN;
		}
		List<Folder> folders = folderService.findFolderByParentId(parentId);
		if(ToolUtil.isArrayListNotEmpty(folders)) {
			Folder folder = folders.get(0);
			if(folder.getUserId() != user.getId()) {
				return "404";
			}
		} 
		
		page = folderService.findPageChildrenFolder(page, parentId, new Folder(user.getId()));
		model.addAttribute("page", page).addAttribute("type", TYPE);
		return "file/fileList";
		
	}
	
	@RequestMapping(value = "/childrenFolder", method = RequestMethod.POST)
	public String childrenFolder(Model model, Page<Folder> page, Folder folder){
		User user = getUser();
		if(null == user){
			return REDIRECT_LOGIN;
		}
		page = folderService.findPageChildrenFolder(page, user.getId(), folder);
		model.addAttribute("page", page);
		return "file/query/folderQuery";
		
	}
	
	@RequestMapping(value = "/addFolder", method = RequestMethod.POST)
	public void addFolder(Folder folder){
		User user = getUser();
		if(null == user){
			error("用户未登录");
			return;
		}
		folder.setUserId(user.getId());
		folder.setCreateTime(Utility.getSimpleFormatedDayNow());
		boolean isTrue = folderService.insert(folder);
		if(isTrue) {
			success("文件夹创建成功");
			return;
		}
		error("文件夹创建失败");
	}
	
	@RequestMapping(value = "/updateFolder", method = RequestMethod.POST)
	public void updateFolder(Folder folder){
		User user = getUser();
		if(null == user){
			error("用户未登录");
			return;
		}
		Folder oldFolder = folderService.findFolderById(folder.getId());
		oldFolder.setName(folder.getName());
		boolean isTrue = folderService.insertOrUpdate(oldFolder);
		if(isTrue) {
			success("文件夹更新成功");
			return;
		}
		error("文件夹更新失败");
	}
	
	@RequestMapping(value = "/deleteFolder", method = RequestMethod.POST)
	public void deleteFolder(int id){
		Folder folder = folderService.findFolderById(id);
		if(folder.getFileList().size() > 0){
			error("该文件夹中还存在文件，请删除文件后再操作");
			return;
		}
		boolean isTrue = folderService.deleteFolder(folder);
		if(isTrue){
			success("删除文件夹成功");
			return;
		}
		error("删除文件夹失败");
	}
}
